<?php
// +----------------------------------------------------------------------
// | snake
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai <1902822973@qq.com>
// +----------------------------------------------------------------------
namespace app\common\model;

use think\Model;

class Apply extends Model
{
    public static $status = [
        0 => '审核中',
        1 => '通过',
        2 => '未通过',
    ];

    public function allApply($map, $offset, $limit)
    {
        return $this->where($map)->limit($offset,$limit)->select();
    }

    public function add($data)
    {
        $data['create_time'] = time();
        return  $this->insertGetId($data);
    }

    public function edit($data)
    {
        return $this->update($data);
    }

    // 获取排名
    public function getRank()
    {
        return static::where(['aid' => $this->aid, 'poll' => ['gt', $this->poll]])->count() + 1;
    }

    // 获取投票人数
    public function getVoterTurnout()
    {
        return Record::where(['apply_id' => $this->id])->count('distinct(user_id)');
    }

    public function brokerage()
    {
        return $this->hasOne('ApplyBrokerage', 'apply_id');
    }

    // 关联用户
    public function user()
    {
        return $this->belongsTo('User', 'user_id');
    }

    // 关联活动
    public function activity()
    {
        return $this->belongsTo('Activity', 'aid');
    }
}
